<?php 
require dirname(dirname(__FILE__)).'/HapTemplate.php';

class HapTemplate_Tag_Static extends HapTemplate_Tag
{
	var $type = HapTemplate::TYPE_TAG;
	function __toString()
	{
		return '<?php echo $this->StaticUrl(\''.parent::__toString().'\')?>';
	}
}

/**
 * cdn 图片
 * @author ronnie
 * @example
 * {image id="$user.avatar" key="per_16" watermark="true" lazyload="true"}{/image}
 *
 */
class HapTemplate_Tag_Image extends HapTemplate_Tag
{
	var $type = HapTemplate::TYPE_TAG;
	private $lazyTag;
	
	function __construct($content = NULL)
	{
		parent::__construct($content);
		//$this->lazyTag = Conf::get('site.lazy_tag', 'hll');
		$this->lazyTag = 'hll';
	}
	
	function __toString()
	{
		$attrs = parent::__toString();
		
		$params = $this->getParams();
		$key = '';
		$lazyLoad = 'FALSE';
		$watermark = 'FALSE';
		
		if (!isset($params['id'])) {
			throw new Exception('haptemplate_tag_cdn.u_idNotSupported');
		}
		
		if (isset($params['key'])) {
			$key = $params['key'];
		}
		if (!empty($params['lazyload']) && strtolower($params['lazyload']) != 'false') {
			$lazyLoad = 'TRUE';
		}
		if (!empty($params['watermark']) && strtolower($params['watermark']) !== 'false') {
			$watermark = 'TRUE';
		}
		if ($lazyLoad == 'TRUE') {
			return "<img {$this->lazyTag}=\"<?php echo \$this->StaticUrl({$params['id']}, '{$key}', {$watermark})?>\" {$attrs}/>";
		} else {
			return "<img src=\"<?php echo \$this->StaticUrl({$params['id']}, '{$key}', {$watermark})?>\" {$attrs}/>";
		}
	}
}

/**
 * 
 * 数据源
 * @example 
 * <code>
 * {datasource from="bbs.hotTopics" item="hotTopics"}{params forum_id="65" asc="true" keyword="婚纱摄影"}{/params}{/datasource}
 * </doce>
 */
class HapTemplate_Tag_Datasource extends HapTemplate_Tag
{
	private $params = array();
	function addChild(HapTemplate_Tag $child)
	{
		if ($child->tag != 'params') {
			return;
		}
		
		$this->params = array_merge($this->params, $child->getParams());
		parent::addChild($child);
	}
	
	function __toString()
	{
		$params = $this->getParams();
		if (!isset($params['source'])) {
			throw new Exception('haptemplate_tag_datasource.u_fromNotSupplied');
		}
		
		if (!isset($params['item'])) {
			throw new Exception('haptemplate_tag_datasource.u_itemNotSupplied');
		}
		
		return '<?php $this->DataSource(\''.$params['source'].'\',\''.$params['item'].'\','.var_export($this->params, true).')?>';
	}
}

class HapTemplate_Tag_Datasource_Params extends HapTemplate_Tag
{
	function __toString()
	{
		return '';
	}
}

HapTemplate::registerSupportTag('static');
HapTemplate::registerSupportTag('image');
HapTemplate::registerSupportTag('datasource', array('params'));

$ht = new HapTemplate();
$content = <<<HTML
<!doctype html>
<head>
{include}/common/tpl/meta.phtml{/include}
</head>
<body>
{include}/common/tpl/header.phtml{/include}
{datasource source="bbs.hotTopics" item="hotTopics"}{params forum_id="23" city_id="310900" num="20"}{/params}{/datasource}
啊啊啊
{notempty \$user}
	<ul>
		{cycle source=\$users item=user}
		<li>
			{\$counter}
			<a href="/user/{\$user.uid}">{\$user.uname}</a>
			{image id="\$user.avatar" watermark="true" lazyload="true" key="waterfall"}alt="{\$user.uname}" width="300" height="200"{/image}
			{switch \$user.sex}
				{default}未知{/default}
				{case 1}男{/case}
				{case 2}女{/case}
			{/switch}
		</li>
		{/cycle}
	</ul>
{/notempty}
{empty \$user.avatar}/static/img/default/user.png{/empty}
{include}/common/tpl/footer.phtml{/include}
</body>
</html>
HTML;
$result = $ht->compile($content);
$ht->assign(array(
	'user' => array(
		'avatar' => 10000,
	),
	'users' => array(
		array(
			'uid' => 100,
			'uname' => 'ronnie',
			'sex' => 1,
			'avatar' => 10003,
		),
		array(
			'uid' => 101,
			'uname' => 'petty',
			'sex'  => 2,
			'avatar' => 10003,
		),
		array(
			'uid' => 102,
			'uname' => 'tony',
			'sex'  => 0,
			'avatar' => 10004,
		),
	),
));
var_dump($content, $result);
//var_dump($ht->output($result));
?>